/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.date.DateUtil
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  de.felixroske.jfxsupport.GUIState
 *  javafx.collections.FXCollections
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.ChoiceBox
 *  javafx.scene.control.ScrollPane
 *  javafx.scene.control.TitledPane
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.Pane
 *  javafx.stage.Stage
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.GysApplication;
import com.gys.common.business.DailyReconcileUtil;
import com.gys.common.business.GysUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CacheUtil;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.SyncController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.dataManger.TokenManger;
import com.gys.controller.index.IndexController;
import com.gys.controller.index.IndexHeadController;
import com.gys.data.dailyReconcile.DailyReconcileInData;
import com.gys.data.example.GetLoginStoreOutData;
import com.gys.data.example.GetUserOutData;
import com.gys.view.StoreView;
import com.gys.view.index.IndexView;
import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.GUIState;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.collections.FXCollections;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TitledPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class StoreController
extends BaseController
implements Initializable {
    @FXML
    private ChoiceBox<String> franc;
    @FXML
    private Pane storePane;
    @Autowired
    private IndexHeadController indexHeadController;
    @Autowired
    private IndexController indexController;
    @Autowired
    private SyncController syncController;
    @FXML
    public ScrollPane scrollContentPane;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.initStoreList();
    }

    public void initStoreList() {
        if (ObjectUtil.isEmpty(this.franc)) {
            return;
        }
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                StoreView view = SpringUtil.getBean(StoreView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.ENTER) {
                            StoreController.this.loginAction();
                        }
                        e.consume();
                    }
                });
            }
        }, 500L);
        List storeList = (List)CacheUtil.get("storeList");
        ArrayList<String> nameList = new ArrayList<String>();
        for (int i = 0; i < storeList.size(); ++i) {
            nameList.add(((GetLoginStoreOutData)storeList.get(i)).getStoName());
        }
        this.franc.setItems(FXCollections.observableArrayList(nameList));
        this.franc.getSelectionModel().selectedIndexProperty().addListener((ov, old_value, new_value) -> {
            if (new_value.intValue() != -1) {
                CacheUtil.put("store", storeList.get(new_value.intValue()));
            }
        });
        this.franc.getSelectionModel().selectFirst();
    }

    @FXML
    void loginAction() {
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        if (ObjectUtil.isEmpty(((GetLoginStoreOutData)CacheUtil.get("store")))) {
            alert.setContentText("\u8bf7\u9009\u62e9\u95e8\u5e97");
            alert.showAndWait();
            return;
        }
        HashMap<String, String> param = new HashMap<String, String>();
        GetUserOutData userOutData = (GetUserOutData)CacheUtil.get("logInUser");
        param.put("userId", userOutData.getUserId());
        param.put("stoCode", userOutData.getDepId());
        param.put("client", userOutData.getClient());
        HttpJson httpJson = GysUtil.chooseStore(param);
        if (!httpJson.isSuccess()) {
            alert.setContentText(httpJson.getMsg());
            alert.showAndWait();
            return;
        }
        GetUserOutData loginUser = (GetUserOutData)JSON.parseObject((String)httpJson.getMsg(), GetUserOutData.class);
        loginUser.setAccount(loginUser.getUserId());
        String token = loginUser.getToken();
        TokenManger.setToken(token);
        CacheUtil.put(token, loginUser);
        CacheUtil.put("storeList", loginUser.getStoreList());
        CacheUtil.put("logInUser", loginUser);
        String msg = "";
        DailyReconcileInData inData = new DailyReconcileInData();
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(5, -1);
        inData.setGpdhSaleDate(DateUtil.format((Date)c.getTime(), (String)"yyyyMMdd"));
        HttpJson rsp = DailyReconcileUtil.checkDailyReconcileInfo(inData);
        msg = rsp.getMsg();
        GUIState.getStage().getScene().setOnKeyReleased(null);
        this.indexHeadController.init();
        this.indexController.init();
        GysApplication.showView(IndexView.class);
        this.storePane.getScene().getWindow().hide();
        this.syncController.syncData();
        if (msg.contains("\u672a\u505a\u65e5\u7ed3\u5bf9\u8d26")) {
            alert.setContentText(rsp.getMsg());
            alert.showAndWait();
            this.expand("finMamager", "dailyReconcile");
        }
    }

    private void expand(String userDataParent, String userDataChild) {
        Node node = IndexController.nodeMap.get(userDataParent);
        if (!((TitledPane)node).isExpanded()) {
            ((TitledPane)node).setExpanded(true);
        }
        this.indexController.expandedOther(node);
        this.indexController.changeView(userDataChild);
    }
}

